﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.membercenter.shop.shop_administer
{
    public partial class shop_friendship_edit : System.Web.UI.Page
    {
        YXShop.BLL.Accessories.Hailhellowlink linkBll = new YXShop.BLL.Accessories.Hailhellowlink();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                InitWebControl();
                string id = ChangeHope.WebPage.PageRequest.GetQueryString("linkId");
                if(id!=null&&id!="")
                {
                    ViewState["linkId"] = id;
                    InitModel(Convert.ToInt32(id));
                }
            }
        }
        #region 验证
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtName,"链接名称不能为空","isnull","必填","该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtUrl,"链接地址不能为空","isnull","必填","该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtFirst,"优先级数字越大优先越前，必须是数子","isint","必填","该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtSynopsis,"简介不能为空","isnull","必填","该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtNumber,"点击数必须是数字","isint","","");
            this.Form.Attributes.Add("onsubmit","return CheckForm()");
        }
        #endregion


        protected void btnChange_Click(object sender, EventArgs e)
        {
            SaveOrUpdate();
        }

        private void SaveOrUpdate()
        {
           
            //YXShop.Model.Accessories.Hailhellowlink model =new YXShop.Model.Accessories.Hailhellowlink();
            YXShop.Model.Accessories.Hailhellowlink model = null;
            if (Session["MemberID"]!=null)
            {

                if (ViewState["linkId"]!=null)
                {
                    model = linkBll.GetModelByID(Convert.ToInt32(ViewState["linkId"]));
                    model.UpdateDate = Convert.ToDateTime(System.DateTime.Now);
                }
                else
                {
                    model = new YXShop.Model.Accessories.Hailhellowlink();
                    model.CreateDate = Convert.ToDateTime(System.DateTime.Now);
                    model.UpdateDate = Convert.ToDateTime(System.DateTime.Now);
                }
                model.SiteName = this.txtName.Text.Trim().ToString();
                model.SiteUrl = this.txtUrl.Text.Trim().ToString();
                model.SiteLogo = this.txtLogo.Text.Trim().ToString();
                model.SiteLevel = ChangeHope.Common.ValidateHelper.IsNumber(this.txtFirst.Text.Trim().ToString()) == true ? Convert.ToInt32(this.txtFirst.Text.Trim()) : 0; //判断
                model.SiteContent = this.txtSynopsis.Text.Trim().ToString();
                model.SiteLinkType = Convert.ToInt32(this.rblType.SelectedValue);
                model.SiteState = Convert.ToInt32(this.rblState.SelectedValue);
                model.SiteClickCount = ChangeHope.Common.ValidateHelper.IsNumber(this.txtNumber.Text.Trim()) == true ? Convert.ToInt32(this.txtNumber.Text.Trim()) : 0;                         
                model.PutoutID = Convert.ToInt32(Session["MemberID"]);
                model.PutoutTyID = 1;
                if (ViewState["linkId"] != null)
                {
                    try
                    {
                        linkBll.Amend(model);
                        this.ltlMsg.Text = "操作成功，已修改该信息";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionOk";

                    }
                    catch (Exception exe)
                    {
                        this.ltlMsg.Text = "操作失败，" + exe.Message;
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                    }


                }
                else
                {
                    int count = linkBll.Add(model);
                    if (count > 0)
                    {
                        this.ltlMsg.Text = "操作成功，已添加该信息";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionOk";
                    }
                    else
                    {
                        this.ltlMsg.Text = "操作失败";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                    }
                }
                
            }
        }
        private void InitModel(int id)
        {
            YXShop.Model.Accessories.Hailhellowlink model = linkBll.GetModelByID(id);
            if(model!=null)
            {
                this.txtName.Text=model.SiteName;
                this.txtUrl.Text = model.SiteUrl;
                this.txtLogo.Text = model.SiteLogo;
                this.txtFirst.Text = model.SiteLevel.ToString();
                this.txtSynopsis.Text = model.SiteContent.ToString();
                this.rblType.SelectedValue = model.SiteLinkType.ToString();
                this.rblState.SelectedValue = model.SiteState.ToString();
                this.txtNumber.Text = model.SiteClickCount.ToString();
            }
        }
    }
}
